package cn.cdtu.tibetplan.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import cn.cdtu.tibetplan.pojo.PageEnd;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.Policy;
import cn.cdtu.tibetplan.pojo.Result;
import cn.cdtu.tibetplan.service.SchoolService;
import cn.cdtu.tibetplan.util.Tools;
import cn.cdtu.tibetplan.service.PolicyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;
    @Autowired
    private SchoolService schoolService;

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<Policy> findAll() {
        return policyService.findAll();
    }

    @RequestMapping("/list")
    public String list() {
        return "redirect:search?currentPage=1&pageSize=10";
    }

    @RequestMapping("/search")
    public String search(Policy policy, int currentPage, int pageSize, Model model) {
        PageResult result = policyService.findPage(policy, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "/Policy/index";
    }

    @RequestMapping("/addPolicy")
    public String addPolicy() {
        return "/Policy/add";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model) {
        Policy policy = policyService.findOne(id.longValue());
        String schoolname = schoolService.findOne(policy.getSchoolid().longValue()).getSchoolname();
        model.addAttribute("schoolname", schoolname);
        model.addAttribute("show", policy);
        return "/Policy/show";
    }

    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        Policy policy = policyService.findOne(id.longValue());
        model.addAttribute("edit", policy);
        return "/Policy/edit";
    }

    /**
     * 增加
     *
     * @param policy
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Policy policy) {
        try {
            policyService.add(policy);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param policy
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Policy policy) {
        try {
            policy.setUpdatetime(Tools.getTime());
            policyService.update(policy);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public Policy findOne(Long id) {
        return policyService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            policyService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/addPolicyFile")
    public String addPolicyFile(String title, MultipartFile policyFile){
        MultipartFile blFile = policyFile;
        if(!blFile.isEmpty()){
            String oldFileName = blFile.getOriginalFilename();
            String path = "D:/upload";
//            String randomStr = UUID.randomUUID().toString();
//            String newFileName = randomStr + oldFileName.substring(oldFileName.lastIndexOf("."));
            File file = new File(path,oldFileName);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            try {
                blFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Policy p=new Policy();
            p.setTitle(title);
            p.setPath("/upload/"+oldFileName);
            p.setUpdatetime(Tools.getTime());
            p.setCreatetime(Tools.getTime());
            p.setOperator("admin");
            //设置时间 操作员
            policyService.add(p);
        }
        return "redirect:search?currentPage=1&pageSize=10";
    }

}
